//
//  DISBMProfitViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/5.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISBMProfitViewController.h"
#import "SCChart.h"
#import "DISAProfitList.h"
#import "ZYLookLeaderboardViewController.h"
#import "JiHuoGuanLiYuanAlertView.h"
#import "BActivateAdministratorVC.h"

@interface DISBMProfitViewController ()<SCChartDataSource>
{
    NSIndexPath *path;
    SCChart *chartView;
    int ind ;
    
}
@property (weak, nonatomic) IBOutlet UILabel *toDayProfitLB;
@property (weak, nonatomic) IBOutlet UILabel *dangqianProfitLB;
@property (weak, nonatomic) IBOutlet UILabel *leijiProfitLB;
@property (weak, nonatomic) IBOutlet UIView *zhexiantuBGview;
@property (weak, nonatomic) IBOutlet UIButton *seleBut;
@property (nonatomic,strong) NSMutableArray *muarr;
@property (weak, nonatomic) IBOutlet UIImageView *bgviewmy;
@property (nonatomic,strong) NSString *typeStr;//选择时间类型
@property (weak, nonatomic) IBOutlet UIButton *LookBtn;

@property (nonatomic,strong) UIButton *selebut;
@property (nonatomic,strong)JiHuoGuanLiYuanAlertView *alertView;
@end

@implementation DISBMProfitViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [self preferredStatusBarStyle];
    if (chartView) {
        [self requestDetaile];
        [self requestcountmy];
    }
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    // 返回你所需要的状态栏样式
    return UIStatusBarStyleLightContent;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    //状态栏字体黑色
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    
    UIImageView *myimage = [[UIImageView alloc]initWithFrame:CGRectMake(0, KHEIGHT-kTabbarHeight-10, KWIDTH, 10)];
    myimage.image = imgname(@"shouyiyinyingimg");
    [self.view addSubview:myimage];
    self.muarr = [NSMutableArray arrayWithCapacity:1];
    [self requestDetaile];
    
    
    [self.view sendSubviewToBack:self.bgviewmy];
    
    [self requestcountmy];
}
-(void)requestcountmy{

    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        if ([[[dic objectForKey:@"data"] objectForKey:@"is_power"]  integerValue]== 0 ) {
            
            [self showJiHuoHuiYuanAlertView];

        }else{
          
        }
        
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:YES];
    
}
//弹出 市县级激活会员身份view
-(void)showJiHuoHuiYuanAlertView
{
    if (_alertView) {
        [_alertView removeFromSuperview];
    }
    _alertView = [[JiHuoGuanLiYuanAlertView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT)];
    [_alertView.activeBtn addTarget:self action:@selector(goActiveAdminVC) forControlEvents:UIControlEventTouchUpInside];
    [[UIApplication sharedApplication].keyWindow addSubview:_alertView];
    
}
-(void)goActiveAdminVC
{
    [_alertView removeFromSuperview];
    BActivateAdministratorVC *vc = [[BActivateAdministratorVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];

}
-(void)showzhexintu{
    
    if (chartView) {
        [chartView removeFromSuperview];
        chartView = nil;
    }
    chartView = [[SCChart alloc] initwithSCChartDataFrame:CGRectMake(0, 0, KWIDTH -10, self.zhexiantuBGview.height)
                                               withSource:self
                                                withStyle:SCChartLineStyle];
    chartView.backgroundColor = [UIColor colorWithHexString:@"#FDFDFD"];
        [chartView showInView:self.zhexiantuBGview];


}

-(void)requestDetaile{
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"dateType"] = _typeStr;
    
    [NetWorkTool POST:shouyeShouyiApi param:param success:^(id dic) {
        KMyLog(@"________%@",dic);
        [self.muarr removeAllObjects];
        NSDictionary *mydic = [dic objectForKey:@"data"];
        self.leijiProfitLB.text =[NSString stringWithFormat:@"%@",[mydic objectForKey:@"AllEar"]];
        self.dangqianProfitLB.text = [NSString stringWithFormat:@"%@",[mydic objectForKey:@"currentEar"]];
        self.toDayProfitLB.text = [NSString stringWithFormat:@"%@",[mydic objectForKey:@"todayEar"]];
        
        for (NSDictionary *moDic in [mydic objectForKey:@"list"]) {
            DISAProfitList *model = [[DISAProfitList alloc]init];
            [model setValuesForKeysWithDictionary:moDic];
            NSString *str = [[moDic objectForKey:@"data"] substringFromIndex:3];
            [self.muarr addObject:str];
        }
        [self showzhexintu];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.muarr removeAllObjects];
        [self showzhexintu];

    } fail:^(NSError *error) {
        [self.muarr removeAllObjects];
        [self showzhexintu];
    } needUser:YES];
}

- (IBAction)seleButton:(UIButton *)sender {
    NSString *str = [USER_DEFAULT objectForKey:@"is_power"];
    if ([str isEqualToString:@"0"]) {
        ShowToastWithText(@"请到个人信息激活管理员身份");
        return;
    }
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"选择时间" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    /*
     typedef NS_ENUM(NSInteger, UIAlertActionStyle) {
     UIAlertActionStyleDefault = 0,
     UIAlertActionStyleCancel,         取消按钮
     UIAlertActionStyleDestructive     破坏性按钮，比如：“删除”，字体颜色是红色的
     } NS_ENUM_AVAILABLE_IOS(8_0);
     
     */
    // 创建action，这里action1只是方便编写，以后再编程的过程中还是以命名规范为主
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"本日" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.typeStr = @"day";
        [sender setTitle:@"本日" forState:(UIControlStateNormal)];
        [self requestDetaile];
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"本周" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [sender setTitle:@"本周" forState:(UIControlStateNormal)];
        self.typeStr = @"wek";
        [self requestDetaile];
    }];
    
    UIAlertAction *action3= [UIAlertAction actionWithTitle:@"本月" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [sender setTitle:@"本月" forState:(UIControlStateNormal)];
        self.typeStr = @"mon";
        [self requestDetaile];
    }];
    UIAlertAction *action4= [UIAlertAction actionWithTitle:@"本年" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [sender setTitle:@"本年" forState:(UIControlStateNormal)];
        self.typeStr = @"yea";
        [self requestDetaile];
    }];
    
    UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    
    //把action添加到actionSheet里
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    [actionSheet addAction:action3];
    [actionSheet addAction:action4];
    [actionSheet addAction:action5];
    [self presentViewController:actionSheet animated:YES completion:nil];
}

//查看排行榜按钮点击事件
- (IBAction)lookBtnAction:(id)sender {
    ZYLookLeaderboardViewController *lookvc = [[ZYLookLeaderboardViewController alloc] init];
    [self.navigationController pushViewController:lookvc animated:YES];
}

#pragma mark - @optional
//颜色数组
- (NSArray *)SCChart_ColorArray:(SCChart *)chart {
//    return @[SCGreen,SCRed,[UIColor redColor]];
    return @[[UIColor colorWithHexString:@"#70BE68"],SCRed,[UIColor redColor]];

}
#pragma mark - @required
//横坐标标题数组
- (NSArray *)SCChart_xLableArray:(SCChart *)chart {
    NSMutableArray *arrr = [NSMutableArray arrayWithCapacity:1];
    for (int i = 1; i <( _muarr.count+1); i++) {
        NSString *ster = [NSString stringWithFormat:@"%d",i];
        [arrr addObject:ster];
    }
    return arrr;
}

//数值多重数组
- (NSArray *)SCChart_yValueArray:(SCChart *)chart {
    return @[_muarr];
}

- (BOOL)SCChart:(SCChart *)chart ShowMaxMinAtIndex:(NSInteger)index{
    return YES;
}

- (IBAction)jintiAction:(UIButton *)sender {
    NSString *str = [USER_DEFAULT objectForKey:@"is_power"];
    if ([str isEqualToString:@"0"]) {
        ShowToastWithText(@"请到个人信息激活管理员身份");
        return;
    }
    //今日收益
    DISBproSeleListViewController *upvc = [[DISBproSeleListViewController alloc]init];
    upvc.moneyStr = self.toDayProfitLB.text;
    upvc.typeSYstr = @"1";
    upvc.titleStr = @"收益明细";
    [self.navigationController pushViewController:upvc animated:YES];
}

- (IBAction)dangqinaAction:(UIButton *)sender {
    NSString *str = [USER_DEFAULT objectForKey:@"is_power"];
    if ([str isEqualToString:@"0"]) {
        ShowToastWithText(@"请到个人信息激活管理员身份");
        return;
    }
    //当前收益
    DISBproListViewController *upvc = [[DISBproListViewController alloc]init];
    upvc.moneyStr = self.dangqianProfitLB.text;
    upvc.typeSYstr = @"2";
    upvc.titleStr = @"收益明细";
    [self.navigationController pushViewController:upvc animated:YES];
}

- (IBAction)leiljishouyiAction:(UIButton *)sender {
    NSString *str = [USER_DEFAULT objectForKey:@"is_power"];
    if ([str isEqualToString:@"0"]) {
        ShowToastWithText(@"请到个人信息激活管理员身份");
        return;
    }
    //累计收益
    DISBproListViewController *upvc = [[DISBproListViewController alloc]init];
    upvc.moneyStr = self.leijiProfitLB.text;
    upvc.typeSYstr = @"3";
    upvc.titleStr = @"收益明细";
    [self.navigationController pushViewController:upvc animated:YES];
}

@end
